//
//  BookItemViewController.m
//  Words
//
//  Created by honeyrr on 2017/10/12.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "BookItemViewController.h"

@interface BookItemViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *bookItemTableView;

@property(nonatomic, strong) NSArray *bookWords;
@end

@implementation BookItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadBookItemData];
}

-(NSArray *)bookWords
{
    if (!_bookWords) {
        _bookWords = [NSArray array];
    }
    return _bookWords;
}

-(void) loadBookItemData
{
    NSString *bookWordsPath = [[NSBundle mainBundle] pathForResource:self.bookItem ofType:nil];
    NSString *bookWordsData = [NSString stringWithContentsOfFile:bookWordsPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *bookWordsArr = [bookWordsData componentsSeparatedByString:@"\n"];
    NSMutableArray *marr = [NSMutableArray new];
    for (NSString *word in bookWordsArr) {
        NSArray *strArr = [word componentsSeparatedByString:@"#"];
        /*
         {
             "en":"hello",
             "zh":"你好"
         }
         */
        NSDictionary *dict = @{@"en" : [strArr firstObject],@"zh":[strArr lastObject]};
        [marr addObject:dict];
    }
    self.bookWords = marr;
    [self.bookItemTableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bookWords.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.bookWords[indexPath.row][@"en"];
    cell.detailTextLabel.text = self.bookWords[indexPath.row][@"zh"];
    return cell;
}

@end
